package com.mall.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.mall.DTO.StatisWareDTO;
import com.mall.service.ICategoryService;
import com.mall.service.IOrderDetailService;
import com.mall.service.IWaresService;
import com.mall.utils.R;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品销售情况统计
 *
 * @author cxx
 * @date 2023/5/12 9:50
 * @return
 */
@RestController
@Slf4j
@SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
public class StatisController {
    @Resource
    private IOrderDetailService orderDeatilService;
    @Resource
    private IWaresService wareService;
    @Resource
    private ICategoryService categoryService;


    /**
     * 统计各种商品销售情况,返回结果中存在商品ID,商品名,商品分类，销售数量,销售总金额
     *
     * @param
     * @return com.mall.utils.R<java.util.List < com.mall.pojo.Wares>>
     * @author cxx
     * @date 2023/5/12 9:58
     */
    @GetMapping("/getwarestatis")

    public R<Object> getWareStatis() {
        List<StatisWareDTO> waresList = orderDeatilService.getWareStatis();
        log.info(waresList.toString());
        return ObjectUtils.isEmpty(waresList) ? R.error("最近没有销售数据") : R.success(waresList);
    }

    /**
     * 根据商品名称模糊分页查询商品销售情况，页码和页大小可以不用传递(不传递则是默认使用1,5)
     *
     * @param page     页码
     * @param pageSize 页大小
     * @param wareName 商品名称
     * @return com.mall.utils.R<java.util.List < com.mall.DTO.StatisWareDTO>>
     * @author cxx
     * @date 2023/6/6 15:31
     */
    @GetMapping("/getWareStatisByName")

    public R<Object> getWareStatisByName(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                         @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                         @RequestParam(name = "wareName", required = false) String wareName) {
        if (StringUtils.isNotBlank(wareName)) {
            List<StatisWareDTO> wares = orderDeatilService.listWareStatisByWareId(wareService.getWareIdByName(wareName), page, pageSize);
            return ObjectUtils.isEmpty(wares.get(0)) ? R.error("该商品还没有被购买过!") : R.success(wares);
        }
        List<StatisWareDTO> waresList = orderDeatilService.getWareStatis();

        return ObjectUtils.isEmpty(waresList) ? R.error("最近没有销售数据!") : R.success(waresList);
    }
}
